<?php

namespace app\api\controller;

use think\Controller;
use think\Request;

class Upload extends Controller
{
    /**
     * @return \think\response\Json
     * 文件上传
     */
    public function uploadImg()
    {
        $file = request()->file('files');
        if ($file) {
              //验证格式
            $info = $file->validate(['size' => 1024 * 1024 * 3, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $imgUrl = $info->getSaveName();
            } else {
                return getJson(500, '文件格式不对');
            }
        } else {
            return getJson(500, '文件未上传');
        }
        return getJson(200, '成功', $imgUrl);
    }
}
